import 'package:flutter/material.dart';
import 'package:flutter_syt/generated/l10n.dart';
import 'package:flutter_syt/theme/theme_color.dart';
import 'package:flutter_syt/utils/click_utils.dart';

class SexSelect extends StatefulWidget {
  final Function(int)? onChange;
  final int initValue;
  const SexSelect(this.onChange, this.initValue);

  @override
  _SexSelectState createState() => _SexSelectState();
}

class _SexSelectState extends State<SexSelect> {
  int _selected = 0;

  @override
  void initState() {
    super.initState();
    setState(() {
      _selected = widget.initValue;
    });
  }

  Widget _sexContainer(int sex) {
    String icon = 'sex_male';
    if (sex == 2) {
      icon = 'sex_female';
    }
    if (_selected == sex) {
      icon = '${icon}_select';
    }
    return GestureDetector(
      onTap: ClickUtils.debounce(() {
        setState(() {
          _selected = sex;
          widget.onChange!(sex);
        });
      }),
      child: Container(
        child: Column(
          children: [
            Container(
              alignment: Alignment.center,
              margin: EdgeInsets.only(bottom: 12),
              child: Image(
                image: AssetImage("assets/icons/$icon@2x.png"),
                width: 72,
                height: 72,
              ),
            ),
            Text(
              sex == 1 ? S.of(context).male : S.of(context).female,
            )
          ],
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.all(20),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          _sexContainer(1),
          _sexContainer(2),
        ],
      ),
    );
  }
}
